package com.marketlive.entity.generator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.hibernate.type.Type;
import org.hibernate.dialect.Dialect;
import org.hibernate.MappingException;
import org.hibernate.HibernateException;
import org.hibernate.id.IdentifierGenerationException;
import org.hibernate.engine.SessionImplementor;

import java.util.Properties;
import java.io.Serializable;

import com.marketlive.entity.IntegerPk;

/**
 * Generates <code>IntegerPk</code>s using the specified MAX_ID table.
 * ID generation is configured in the HBM file for the entity.
 */
public class MaxIDIntegerPkGenerator extends MaxIDIdentifierGenerator {

    /**
     * Logger.
     */
    private static Log log = LogFactory.getLog(MaxIDIntegerPkGenerator.class);

    /**
     * Constant representing the property name for entity type.
     */
    public static final String ENTITY_TYPE = "ENTITY_TYPE";

    /**
     * The entity type used to construct the IntegerPk.
     */
    private String entityType;

    /**
     * Reads the generator parameters from HBM metafile and stores them in the generator object.
     * This method is invoked by Hibernate.
     *
     * @param type       the type of entity to generate identifiers for
     * @param properties the properties to use for the generator
     * @param dialect    the database information
     * @throws org.hibernate.MappingException if an error occurs
     * @throws RuntimeException if the entity type for this generator is unknown
     *
     */
    public void configure(final Type type, final Properties properties, final Dialect dialect)
            throws MappingException {
        super.configure(type, properties, dialect);
        this.entityType = properties.getProperty(ENTITY_TYPE);
        if (this.entityType == null) {
            throw new RuntimeException("Could not parse ENTITY_TYPE parameter in MaxID IntegerPK Generator.");
        }
    }

    /**
     * Generates an <code>IntegerPk</code> using the MAX ID table and the given session.
     * This method is provided for use by the persistence mechanism.
     *
     * @param session a <code>SessionImplementor</code> that gives access to a <code>ConnectionProvider</code>
     * @param entity  the entity to generate the identity for
     * @return an <code>IntegerPk</code> for the given entity
     * @throws org.hibernate.id.IdentifierGenerationException if the identity could not be generated or the transaction
     *         gracefully terminated
     *
     * @throws org.hibernate.HibernateException
     * @see com.marketlive.entity.IntegerPk
     */
    public Serializable generate(final SessionImplementor session, final Object entity)
            throws IdentifierGenerationException, HibernateException {
        Integer id = (Integer) super.generate(session, entity);
        return new IntegerPk(entityType, id.intValue());
    }
}
